package com.qf.controller;

import com.qf.pojo.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Wy
 * @version V1.0
 * @Project MavenDemo
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/13 21:14
 */
@Controller
public class LoginController {
    @RequestMapping("/doLogin1")
    public String dologin(String username,String password,boolean rememberMe){
        //加密传来的密码
        Md5Hash md5Hash = new Md5Hash(password, username, 1024);
        String x= md5Hash.toString();
        //2. 将页面输入的用户名, 密码封装成Shiro需要的用户名密码对象
        UsernamePasswordToken token = new UsernamePasswordToken(username, x);
        //3. 判断是否需要记住我, 如果是, 那么设置记住我选项
        if(rememberMe){
            token.setRememberMe(true);
        }
        //4. 获取Subject对象, 这个是Shiro框架的, 当前登录用户对象
        Subject subject = SecurityUtils.getSubject();

        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            return "/login.jsp";
        }catch (IncorrectCredentialsException e){
            return "/login.jsp";
        }
        catch (AuthenticationException e) {
           return  "/login.jsp";
        }


        return "/index.jsp";

    }
}
